package com.allianz.webapps.secondController;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.misc.BASE64Decoder;

import com.allianz.frame.utils.ApplicationContextUtil;
import com.allianz.frame.utils.ImageUploadUtil;
import com.allianz.frame.utils.PropertiesUtil;
import com.allianz.frame.utils.StringUtil;
import com.allianz.webapps.https.interfaces.type001.CoverInit;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.https.interfaces.util.HttpsUtil;
import com.allianz.webapps.https.interfaces.util.ObjectUtil;
import com.allianz.webapps.orderspojo.AppBase;
import com.allianz.webapps.orderspojo.CP;
import com.allianz.webapps.orderspojo.ManagerSearch;
import com.allianz.webapps.quopojo.QuoCreateModel;
import com.allianz.webapps.service.impl.ImageUploadService;
import com.allianz.webapps.vo.UploadVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@Controller
@RequestMapping(value="LineNote")
public class LineNoteController {
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private ObjectUtil objectUtil;
	
	@RequestMapping(value="index")
	public String index(HttpServletRequest request,HttpServletResponse response){
		return "views/page/lineNote";
	}
	/* 单个上传到临时文件夹
	 * param  preprintedNo投保单号  policyRef保单号   
	 *		  photoType照片类型           imageData照片数据
	 * ps: photoType需包括type和所处type类的位置
	 */
	@RequestMapping(value="uploadOne")
	@ResponseBody
	public String upload_one(HttpServletRequest request,HttpServletResponse response) throws IOException{
		//获得数据
		String jsonParam = request.getParameter("jsonParam");
		UploadVo obj =objectMapper.readValue(jsonParam,UploadVo.class);
		String preprintedNo = obj.getPreprintedNo();
		String policyRef = obj.getPolicyRef();
		String photoType = obj.getPhotoType();
		String imageData = obj.getImageData();
		imageData = imageData.replaceAll("data:image/png;base64,","");
		//校验数据
		Assert.hasLength(preprintedNo, "投保单号为空");
		Assert.hasLength(policyRef, "保单号为空");
		Assert.hasLength(photoType, "非法照片类型");
		Assert.hasLength(imageData, "非法照片");
		//得到存储的地址
		File file = new File(PropertiesUtil.getResourcePath()+"private/photo_temp/"+preprintedNo+"/");
		if(file.exists()){
			if(file.isFile()){
				file.delete();
			}
		}else{
			file.mkdirs();
		}
		//同一位置照片多次上传 删除照片  
		for(File f:file.listFiles()){
			if(f.getName().contains(photoType)){
				f.delete();
			}
		}
		//data:image/png;base64
		String realPath = PropertiesUtil.getResourcePath()+"private/photo_temp/"+preprintedNo+"/"+photoType+"_"+new Date().getTime()+".jpg";
		if(ImageUploadService.uploadUtil(imageData, realPath)){
			return "Y";
		}
		return "N";
	}
	
	@RequestMapping(value="uploadAll")
	@ResponseBody
	public String all_submit(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response) throws Exception {
		AppBase appBase = objectMapper.readValue(jsonParam,AppBase.class);
			
	//	拍照全部上传的所需数据
		String policyRef=appBase.getPolicyRef();
		String preprintedNo=appBase.getPreprintedNo();
		String ip=ImageUploadUtil.getIP(request);
		List<String> typeList = new ArrayList<String>();
		typeList.add("001");
		typeList.add("002");
		typeList.add("003");
		typeList.add("004");
		typeList.add("005");
		String flagFileString_photo_Directory = PropertiesUtil.getResourcePath()+"private/photo_temp/"+preprintedNo+"/";
	//  上传照片 返回boolean值	
		boolean bo=ImageUploadService.imageUploadMethod(policyRef, preprintedNo, typeList, flagFileString_photo_Directory, ip,null,null);
		if(bo){
			return "Y";
		}
		return "N";
	}
}